<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC3012 – In POSIX sh, lexicographical `\<` is undefined.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC3012 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC3012">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="in-posix-sh-lexicographical--is-undefined">In POSIX sh,
lexicographical <code>\&lt;</code> is undefined.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC3012.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">#!/bin/sh</span></span>
<span id="cb1-2"><a href="SC3012.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="va">x</span><span class="op">=</span><span class="st">&quot;aardvark&quot;</span></span>
<span id="cb1-3"><a href="SC3012.html#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="va">y</span><span class="op">=</span><span class="st">&quot;zebra&quot;</span></span>
<span id="cb1-4"><a href="SC3012.html#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="bu">[</span> <span class="va">$x</span> <span class="dt">\&lt;</span> <span class="va">$y</span> <span class="bu">]</span></span>
<span id="cb1-5"><a href="SC3012.html#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="cf">then</span></span>
<span id="cb1-6"><a href="SC3012.html#cb1-6" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$x</span><span class="st"> comes before </span><span class="va">$y</span><span class="st"> in the dictionary&quot;</span></span>
<span id="cb1-7"><a href="SC3012.html#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<p>First, make sure you wanted a lexicographical comparison (aka
dictionary order), and not a numerical comparison.</p>
<p>Then to compare as string, you can use <code>expr</code> and make
sure that the strings are not interpreted numerically by adding some
non-numerical data to them. Here, an apostrophe is prepended:</p>
<pre><code>#!/bin/sh
x=&quot;aardvark&quot;
y=&quot;zebra&quot;
if expr &quot;&#39;$x&quot; \&lt; &quot;&#39;$y&quot;   &gt; /dev/null 
then
  echo &quot;$x comes before $y in the dictionary&quot;
fi</code></pre>
<h3 id="rationale">Rationale:</h3>
<p>The <code>test</code> binary operators <code>&gt;</code>,
<code>\&gt;</code>, <code>&lt;</code>, and <code>\&lt;</code> are not
part of POSIX and not guaranteed to be supported in scripts targeting
<code>sh</code>.</p>
<p>The <code>expr</code> functionality is specified by POSIX.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>If you know your <code>sh</code> will be e.g. <code>dash</code>,
consider explicitly using <code>#!/bin/dash</code>.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><a href="https://mywiki.wooledge.org/Bashism#Conditionals">Greg's
Wiki - Bashism - Conditionals</a></li>
<li>POSIX: <a
href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128">test</a>,
<a
href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/expr.html">expr</a></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


